package com.jinghang.wayofrich.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.jinghang.wayofrich.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 订单页面
 */

public class OrderFragment extends Fragment {
    private SearchView searchView;

    //订单列表
    private ArrayList<HashMap<String, Object>> orderList;
    private SimpleAdapter orderListAdapter;
    private ListView orderListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);

        //设置搜索框的样式
        setViewStyle(view);

        //订单列表
        initOrders(view);
        return view;
    }

    /**
     * 设置搜索框样式
     * @param view
     */
    private void setViewStyle(View view) {
        searchView = view.findViewById(R.id.searchOrder);
        if (searchView != null) {
            try { //--拿到字节码
                Class<?> argClass = searchView.getClass();
                //指定某个私有属性,mSearchPlate是搜索框父布局的名字
                Field ownField = argClass.getDeclaredField("mSearchPlate");
                //暴力反射,只有暴力反射才能拿到私有属性
                ownField.setAccessible(true);
                View mView = (View) ownField.get(searchView);
                //设置背景
                mView.setBackgroundResource(R.drawable.searchview_line);

                //获取到TextView的控件
                int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null,null);
                TextView textView = (TextView) searchView.findViewById(id);
                //设置字体大小为14sp
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);//14sp
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 初始化订单
     * @param view
     */
    private void initOrders(View view){
        //获取文章
        getOrders(view);
        orderListAdapter = new SimpleAdapter(getContext(), orderList,// 数据源
                R.layout.fragment_order_list,// ListItem的XML实现
                // 动态数组与ImageItem对应的子项
                new String[]{"orderBank","orderId","orderTime","orderUserName","orderMobile","orderStatus"},
                // ImageItem的XML文件里面的一个ImageView,TextView ID
                new int[]{R.id.orderBank,R.id.orderId,R.id.orderTime,R.id.orderUserName,R.id.orderMobile,R.id.orderStatus});
        // 添加并且显示
        orderListView.setAdapter(orderListAdapter);
        // 添加菜单点击
        orderListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                for (int i = 0; i < orderList.size(); i++) {
                    if (arg2 == i) {
                        Toast.makeText(getContext(),
                                orderList.get(arg2).get("orderId") + "    " + arg2, Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            }
        });
    }

    /**
     * 获取订单数据
     */
    private void getOrders(View view){
        orderListView = view.findViewById(R.id.orderList);
        orderList = new ArrayList();
        for (int i=1;i<50;i++){
            HashMap<String, Object> map = new HashMap();
            map.put("orderBank","广发银行");
            map.put("orderId","1047238712344568");
            map.put("orderTime", "2018-10-29 13:57:42");
            map.put("orderUserName", "徐之清");// 图像资源的ID
            map.put("orderMobile", "18888888888");// 图像资源的ID
            map.put("orderStatus", "等待银行审批");// 图像资源的ID
            orderList.add(map);
        }
    }
}
